package com.bdqn.controller;


import com.bdqn.entity.ShoppingCart;
import com.bdqn.param.requset.CartParam;
import com.bdqn.service.ShoppingCartService;
import com.bdqn.service.UserService;
import com.bdqn.util.Constants;
import com.bdqn.util.RespBean;
import com.bdqn.util.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author hunter
 * @since 2022-06-11
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public RespBean list() {
        List<ShoppingCart> list = shoppingCartService.list();
        return list != null ? RespBean.ok("获取购物车信息成功", list) : RespBean.error("获取购物车信息失败");

    }

    /**
     * 添加购物车
     *
     * @return
     */
    @PostMapping("/add")
    public RespBean add(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
        String phone = (String) session.getAttribute(Constants.LOGIN_SESSION);
        if (StringUtils.isEmpty(phone)) {
            return new RespBean(ResultEnum.PHONE_ISNULL);
        }
        // 进行优化  如果我们重复购买一个商品  则只更新其购买数量
        for (ShoppingCart cart : shoppingCartService.list()) {
            if (Objects.equals(shoppingCart.getDishId(), cart.getDishId())) {
                Integer number = cart.getNumber()+1;
                System.out.println("更新后的数量为"+number);
                boolean flag = shoppingCartService.updateById(cart.setNumber(number).setDishFlavor(cart.getDishFlavor()+"{"+shoppingCart.getDishFlavor()+"}"));
                return RespBean.ok("更新数量成功",flag);
            }
//            ||shoppingCart.getSetmealId()==cart.getSetmealId()
        }
        log.info("通过session存储的手机号 获取用户id: {}", phone);
        boolean save = shoppingCartService.save(shoppingCart.setUserId(userService.selectByPhone(phone).getId()));
        shoppingCartService.add(new CartParam(shoppingCart.getDishId(), shoppingCart.getSetmealId()));
        return save ? RespBean.ok("添加购物车成功", save) : RespBean.error("添加购物车失败");
    }

    /**
     * 清空购物车的操作
     * 方式① 边循环 边单个删除
     * 方式② 循环获取id 存集合  通过集合一并删除
     *
     * @return
     */
    @DeleteMapping("/clean")
    public RespBean clean() {
        try {
            for (ShoppingCart shoppingCart : shoppingCartService.list()) {
                shoppingCartService.removeById(shoppingCart.getId());
            }
            return RespBean.ok("清空购物车成功", 1);
        } catch (Exception e) {
            e.printStackTrace();
            return RespBean.error("清空购物车失败");
        }
    }

    /**
     * 对购物的商品数量进行减除操作
     *
     * @return
     */
    @PostMapping("/sub")
    public RespBean sub(@RequestBody CartParam cartParam) {
        boolean flag=false;
        for (ShoppingCart shoppingCart : shoppingCartService.list()) {
            if(shoppingCart.getNumber()>0){
               flag = shoppingCartService.sub(cartParam);
             }else {
                 return RespBean.error("不能再减啦");
             }
        }
        return flag ? RespBean.ok("-1成功", flag) : RespBean.error("-1失败");
    }

    /**
     * 对购物的商品数量进行+1操作
     * @return
     */

}